<template>
    <div class="container">
        <scroller>
            <div style="flex-direction: row;margin-top: 30px; ">
                <div class="top" style="width: 375px;">
                    <image src="bmlocal://assets/logo.png" style="width: 200px; height: 200px; margin-left: 87px; border-radius: 30px;"></image>
                    <text style="text-align: center; margin-top: 20px;">NB-Iot Toolkit</text>
                </div>
                <div style="flex-direction: column; width: 375px;">
                    <div style="align-content: center; text-align: center">
                        <image src="bmlocal://assets/wx-qrcode.jpg" title="微信" style="width: 200px; height: 200px; margin-left: 87px;"></image>
                        <text style="text-align: center; margin-top: 20px;">微信号：desertsurge</text>
                    </div>
                </div>
            </div>
            <!--<div style="margin-top: 60px; flex-direction: column">
                <wxc-button text="检查更新" @wxcButtonClicked="checkVersion" class="bottom-search-btn"></wxc-button>
            </div>-->
            <div style="margin-top: 60px;">
                <wxc-cell desc="检查更新"
                          :title="`当前版本：${appVersion}`"
                          @wxcCellClicked="checkVersion"
                          :has-arrow="true"></wxc-cell>
                <wxc-cell title="分享给好友"
                          @wxcCellClicked="shareApp"
                          :has-arrow="true"></wxc-cell>
                <wxc-cell desc="所有权"
                          title="涛哥"></wxc-cell>
                <wxc-cell desc="微信号"
                          title="desertsurge"></wxc-cell>
            </div>
        </scroller>
    </div>
</template>

<script>
    import yhq from '../config/yhq';
    const router = weex.requireModule('bmRouter');
    import { WxcButton, WxcCell } from 'weex-ui'

    export default {
        name: "about",
        components:{
            WxcButton, WxcCell
        },
        data() {
            return {
                appVersion: weex.config.env.appVersion,
                appName: weex.config.env.appName
            }
        },
        created() {
        },
        methods: {
            checkVersion(){
                let self = this;
                self.$fetch({
                    method: 'GET',
                    url: yhq.versionServerUrl
                }).then(resp => {
                    console.log(resp)
                    if(!resp.latestVersion){
                        self.$notice.toast('检查版本失败');
                        return;
                    }
                    if(this.compare(resp.latestVersion)){
                        this.$notice.confirm({
                            title: '检查版本',
                            message: `检测到新版本 ${resp.latestVersion}.\n\n${resp.description}\n\n确定要更新吗？`,
                            okTitle: '更新',
                            cancelTitle: '取消',
                            okCallback() {
                                // 点击确认按钮的回调
                                router.openBrowser(resp.apk_url || resp.site);
                            },
                            cancelCallback() {
                                // 点击取消按钮的回调
                                // self.$notice.toast('不更新');
                            }
                        })
                    } else {
                        self.$notice.toast('当前已经是最新版本');
                    }
                });
            },
            compare(latestVersion){
                let appVersion = weex.config.env.appVersion;
                if(latestVersion === appVersion){
                    return false;
                }
                let lvArr = latestVersion.split('.');
                let appVerArr = appVersion.split('.');
                let needUpdate = false;
                lvArr.forEach((lv, i)=>{
                    if(!needUpdate){
                        let av = appVerArr[i];
                        if(lv - av > 0){
                            needUpdate = true;
                        }
                    }
                });
                return needUpdate;
            },
            shareApp(){
                this.$share({
                    title:'NB-Iot淘宝京东优惠券',                                     // 分享出去的 title
                    content:'我正在使用的一款有大量优惠券的App，希望对你有用',                                   // 内容
                    url: yhq.downloadUrl,                                       // 用户点击后跳转 url
                    image: 'http://www.lionsparty.com/app/logo/nbiot.png',                                    // 图片url
                    platforms: ['Pasteboard','WechatSession','WechatTimeLine']     // 选填，不传此属性默认全部，目前支持朋友圈、朋友、复制链接
                }).then(resData => {
                    console.log(resData)
                    // 成功的回调
                }, error => {
                    // 失败的回调
                })
            }
        }
    }
</script>

<style scoped>
    .container {
        flex-direction: column;
    }

    .top {
        flex-direction: column;
    }
</style>
